var myTitle=[],$AUTONEWSID="",$TYPESHARE="";

ACTION = function() {
} 

ACTION.prototype.update = function(newsid,status) {
	var fncSuc = XHR.Delegate(this, this.updateSuc);
	var param = {
		action : "updatenews",
		newsid : newsid,
		status : status
	};
	new XHR("post", fncSuc).send($PN + "/DoAdmin", param);
}

ACTION.prototype.updateSuc = function(r) {
	var xml = new XML(r);
	if (xml.value) {
		alert("Update Success");
	} else {
		alert(xml.msg);
	}
}

ACTION.prototype.updateShare = function(newsid,status) {
	var fncSuc = XHR.Delegate(this, this.updateShareSuc);
	var param = {
		action : "updatenewsshare",
		newsid : newsid,
		status : status
	};
	new XHR("post", fncSuc).send($PN + "/DoAdmin", param);
}

ACTION.prototype.updateShareSuc = function(r) {
	var xml = new XML(r);
	if (xml.value) {
		alert("Update Success");
	} else {
		alert(xml.msg);
	}
}

ACTION.prototype.list = function(source, category) {
	var fncSuc = XHR.Delegate(this, this.listSuc);
	var param = {
		action : "listAutoNews",
		source : source,
		category : category
	};
	new XHR("post", fncSuc).send($PN + "/DoAdmin", param);
}

ACTION.prototype.listSuc = function(r) {
	var xml = new XML(r);
	if (xml.value) {
		var news = xml.get("news");
		var html = "";
		 
		for ( var i = 0; i < news.length; i++) {
			myTitle[news[i].getAttribute("newsid")] = news[i].getAttribute("title");
			
			html += "<div style=\"padding:3px\">"
				+ "<span style=\"cursor:pointer\" onClick=\"btnUpdate('"+news[i].getAttribute("newsid")+"','3')\">[Xoa]</span>&nbsp"
				+ "<span style=\"cursor:pointer\" onclick=\"fillForm('"+news[i].getAttribute("newsid")+"','"+news[i].getAttribute("date")+"','"+news[i].getAttribute("url")+"','"+news[i].getAttribute("category")+"','"+news[i].getAttribute("source")+"')\">"
				+ "<a target=\"_blank\" style=\"text-decoration : none\" href=\""+news[i].getAttribute("url")+"\">[detail] </a>"
				+ news[i].getAttribute("title")
				+ " ("+news[i].getAttribute("date")+")</span>"
				+ "</div>";
		}
		$("auto_newslist").innerHTML = html;
	} else {
		alert(xml.msg);
	}
}

ACTION.prototype.listShare = function(source, category) {
	var fncSuc = XHR.Delegate(this, this.listShareSuc);
	var param = {
		action : "listUserShare"
	};
	new XHR("post", fncSuc).send($PN + "/DoAdmin", param);
}

ACTION.prototype.listShareSuc = function(r) {
	var xml = new XML(r);
	if (xml.value) {
		var news = xml.get("news");
		var html = "";
		 
		for ( var i = 0; i < news.length; i++) {
			myTitle[i] = news[i].getAttribute("title");
			
			html += "<div style=\"padding:3px\">"
					+ "<span style=\"cursor:pointer\" onClick=\"btnUpdateShare('"+news[i].getAttribute("newsid")+"','3')\">[Xoa]</span>&nbsp"
					+ "<span style=\"cursor:pointer\" onclick=\"fillFormShare('"+news[i].getAttribute("newsid")+"','"+i+"','"+news[i].getAttribute("link")+"','"+news[i].getAttribute("userid")+"')\">"
					+ "<a target=\"_blank\" style=\"text-decoration : none\" href=\""+news[i].getAttribute("link")+"\"> [detail] </a>"
					+ news[i].getAttribute("title")+ "</span>"
					+ "</div>";
		}
		$("user_newslist").innerHTML = html;
	} else {
		alert(xml.msg);
	}
}

function btnUpdate(newsid,status) {
	new ACTION().update(newsid, status);
}

function btnUpdateShare(newsid,status) {
	new ACTION().updateShare(newsid, status);
}

function fillForm(newsid,date,url,category,source) {
	$("title").value = myTitle[newsid];
	$("date").value = date;
	$("link").value = url;
	switch (source) {
	case '1': source="Vnexpress";break;
	case '2': source="Dan Tri";break;
	case '3': source="Tuoi Tre";break;
	case '4': source="Thanh Nien";break;
	case '5': source="Tien Phong";break;
	}
	$("cat"+category).checked = true;
	$("source").value = source;
	$AUTONEWSID = newsid;
	$TYPESHARE = "auto";
}

function fillFormShare(newsid,index,link,userid) {
	$("title").value = myTitle[index];
	$("link").value = link;
	//$("source").value = source;
	$AUTONEWSID = newsid;
	$TYPESHARE = "share";
}

function btnList() {
	var source = $("from").value;
	var category = $("category").value;
	new ACTION().list(source, category);
}

function btnListShare() {
	new ACTION().listShare();
}
